package com.xy.frame.controller;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.xy.commons.PageUtil;
import com.xy.frame.model.AjaxJson;
import com.xy.frame.service.ParamService;

/**
 * @author Administrator 参数管理控制器
 */
@Controller
@RequestMapping("/param")
public class ParamController {

	Logger logger = LoggerFactory.getLogger(ParamController.class);
	@Autowired
	ParamService paramService;

	/**
	 * @param currentPage
	 * @param pageSize
	 * @return 查询参数列表数据
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public AjaxJson list(
			HttpServletRequest request,
			@RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
		AjaxJson j = new AjaxJson();
		String paramDesc = request.getParameter("paramDesc");
		Map<String, Object> params = Maps.newHashMap();
		if (!StringUtils.isEmpty(paramDesc)) {
			params.put("paramDesc", paramDesc);
		}
		params.put("start", (currentPage - 1) * pageSize);// > start
		params.put("end", currentPage * pageSize); // <= end
		Map<String, Object> pageList = paramService
				.queryParamListByPage(params);
		pageList.put(PageUtil.CURRENT, currentPage);
		pageList.put(PageUtil.PAGESIZE, pageSize);
		Integer t = Integer.valueOf(pageList.get(PageUtil.TOTALS).toString());
		pageList.put(PageUtil.TOTALPAGE, PageUtil.getTotalPage(t, pageSize));
		j.setObj(pageList);
		if (logger.isDebugEnabled()) {
			logger.debug("请求【/param/list】处理,响应对象【" + j + "】");
		}
		return j;
	}

	/**
	 * @param type
	 * @param model
	 * @return 查看、增加、修改参数页面
	 */
	@RequestMapping(value = "/toParamPage", method = RequestMethod.GET)
	public String toMenuPage(@RequestParam("type") String type, Model model,
			HttpServletRequest request) {

		if (logger.isDebugEnabled()) {
			logger.debug("请求【/metro/param/paramPage】处理页面【/view/metro/param/paramPage.jsp】");
		}
		model.addAttribute("type", type);// 查看-0，增加 -1，修改-2
		if ("2".equals(type)) {
			String paramType = request.getParameter("paramType");
			model.addAttribute("paramType", paramType);
		}
		return "metro/param/paramPage";
	}

	/**
	 * @param menuNum
	 * @return 获取一个参数详细信息
	 */
	@RequestMapping(value = "/getParamType", method = RequestMethod.GET)
	@ResponseBody
	public AjaxJson getParamType(@RequestParam("paramType") String paramType) {
		AjaxJson j = new AjaxJson();

		Map<String, Object> paramMap = paramService.getParamType(paramType);
		j.setObj(paramMap);
		if (logger.isDebugEnabled()) {
			logger.debug("请求【/metro/param/getParamType】处理,响应对象【" + j + "】");
		}
		return j;
	}

	/**
	 * @return 增加类型参数
	 */
	@RequestMapping(value = "/addParam", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson addParam(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		Enumeration<?> keys = request.getParameterNames();
		Map<String, Object> map = Maps.newHashMap();
		while (keys.hasMoreElements()) {
			String reqCode = keys.nextElement().toString();
			map.put(reqCode, request.getParameter(reqCode));
		}
		try {
			paramService.addParamOrUpdate(map);
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("系統忙，新增或更新参数【" + map.get("menuNum") + "】失败!");
			logger.error("新增或者更新参数【" + map.get("menuNum") + "】失败!", e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("请求【/metro/menu/addMenu】处理,响应对象【" + j + "】");
		}
		return j;
	} 
	/**
	 * @return 删除一个参数类型
	 * @throws Exception
	 */
	@RequestMapping(value = "/delParamType", method = RequestMethod.GET)
	@ResponseBody
	public AjaxJson delParamType(@RequestParam("paramType") String paramType) {
		AjaxJson j = new AjaxJson();
		try {
			paramService.deleteParamType(paramType);
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("系統忙，删除参数【" + paramType + "】失败!");
			logger.error("删除参数【" + paramType + "】失败!", e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("请求【/metro/menu/delMenu】处理,响应对象【" + j + "】");
		}
		return j;
	}

	/**
	 * @return 删除一个参数取值
	 * @throws Exception
	 */
	@RequestMapping(value = "/delParamValue", method = RequestMethod.GET)
	@ResponseBody
	public AjaxJson delParamValue(
			@RequestParam("paramValue") String paramValue,
			@RequestParam("paramType") String paramType) {
		AjaxJson j = new AjaxJson();
		try {
			Map<String, Object> params = Maps.newHashMap();
			params.put("paramType", paramType);
			params.put("paramValue", paramValue);
			paramService.deleteParamValue(params);
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("系統忙，删除参数【" + paramType + ":" + paramValue + "】失败!");
			logger.error("删除参数【" + paramType + ":" + paramValue + "】失败!", e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("请求【/metro/param/delParamValue】处理,响应对象【" + j + "】");
		}
		return j;
	}
}